/*
 * channel.cpp
 *
 *  Created on: Aug 1, 2009
 *      Author: zaca
 */

#include <biosignal/montage/channeldata.h>
#include <biosignal/montage/channel.h>
#include <biosignal/signaldata.h>
#include <biosignal/dataframe.h>

#include <tr1/memory>

namespace biosignal {

namespace montage {
using namespace std::tr1;

class BipolarChannelData: public ChannelData {
public:
	BipolarChannelData(const BipolarChannel* channel,
			const DataFrame* data_frame) :
		ChannelData(channel, data_frame) {
		m_source1 = data_frame->signal_source_data(channel->source1());
		m_source2 = data_frame->signal_source_data(channel->source2());
		m_size = m_source1->size();
	}
	virtual double value(unsigned int sample_nr) const {
		double s1 = m_source1->value(sample_nr);
		double s2 = m_source2->value(sample_nr);
		double result = s1 - s2;
		return result;
	}

	virtual int size() const {
		return m_size;
	}

private:
	shared_ptr<const SignalData> m_source1;
	shared_ptr<const SignalData> m_source2;
	int m_size;
};

Channel::~Channel() {
}

std::string Channel::label() const {
	return m_label;
}
Color Channel::color() const {
	return m_color;
}

RefChannel::RefChannel(SourceSignal::SourceSignal_sptr source) : m_source(source) {
	m_label = source->label();
}

double RefChannel::sampleRate() const {
	return m_source->sample_rate();
}


shared_ptr<ChannelData> RefChannel::channel_data(const DataFrame* data_frame) {
	const RefChannel* const_this = const_cast<const RefChannel*>(this);
	RefChannelData * ref_channel_data = new RefChannelData(const_this, data_frame);
	shared_ptr<ChannelData> channel_data(ref_channel_data);
	return channel_data;
}

BipolarChannel::BipolarChannel(std::string source1, std::string source2) :
	m_source1(source1), m_source2(source2) {
	m_label = source1 + " - " + source2;
}
BipolarChannel::~BipolarChannel() {

}

double BipolarChannel::sampleRate() const {
	//TODO completar bien
	return  0;
}

std::string BipolarChannel::source1() const {
	return m_source1;
}
std::string BipolarChannel::source2() const {
	return m_source2;
}

shared_ptr<ChannelData> BipolarChannel::channel_data(
		const DataFrame* data_frame) {

	const BipolarChannel* this_ch = const_cast<const BipolarChannel*> (this);
	BipolarChannelData * bipolar_channel_data = new BipolarChannelData(this_ch,
			data_frame);
	shared_ptr<ChannelData> channel_data(bipolar_channel_data);

	return channel_data;
}

}
}
